package leetcode.slidwidow;

import java.util.HashMap;

public class Lc2981 {

    int ans = -1;
    public int maximumLength(String s) {
        HashMap<String, Integer> map = new HashMap<>();
        int i = 0, j = 0;
        for (; j < s.length(); j++) {
            if (s.charAt(i) != s.charAt(j)) {
                countTimes(s.substring(i, j), map);
                i = j;
            }
        }
        countTimes(s.substring(i, j), map);
        return ans;
    }

    private void countTimes(String temp, HashMap<String, Integer> map) {
        int len = temp.length();
        for (int k = 0; k < len; k++) {
            String sub = temp.substring(k, len);
            map.put(sub, map.getOrDefault(sub, 0) + k + 1);
            if (map.get(sub) >= 3) ans = Math.max(ans, sub.length());
        }
    }

    public static void main(String[] args) {
        Lc2981 lc2981 = new Lc2981();
        System.out.println(lc2981.maximumLength("abcaba"));

    }


}
